using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.IO;


// Set Hook: hookID = SetHook(proc);
//Delete Hook: UnhookWindowsHookEx(hookID);


namespace ClipboardManager
{
    class MainClass : IDisposable
    {
        //GUI
        FormMain form;
        bool formPresent = false;

        //Log Timer
        System.Timers.Timer logTimer;
        public static StringBuilder log = new StringBuilder();

        //Log Writer
        System.IO.FileStream LogFileStream;
        int bytesWritten = 0;
        bool isWritingLog = false;

        public static Bitmap Menu0;
        public static Bitmap Menu1;
        public static Bitmap Menu2;
        public static Bitmap Menu3;
        public static Bitmap MenuTab;

        public MainClass()
        {
            Menu0 = Properties.Resources.menu_black0;
            Menu1 = Properties.Resources.menu_black1;
            Menu2 = Properties.Resources.menu_black2;
            Menu3 = Properties.Resources.menu_black3;
            MenuTab = Properties.Resources.menuTab;

            logTimer = new System.Timers.Timer(10000);
            logTimer.AutoReset = true;
            logTimer.Elapsed += new System.Timers.ElapsedEventHandler(LogToFile);
            logTimer.Start();

            createForm();

            form.ShowInTaskbar = Properties.Settings.Default.ShowTaskbar;

            form.Show();

            Log("Started Clipboard Manager " + DateTime.Now.ToString("H:m:s MM/dd"));
        }

        public static void Log(string text)
        {
#if DEBUG
                Debug.WriteLine(text);
                log.AppendLine(text);
#endif
        }

        void LogToFile(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (log.Length > 0 && !isWritingLog)
            {
                try
                {
                    LogFileStream = new FileStream(@"C:\ClipboardLog.txt", FileMode.Append, FileAccess.Write, FileShare.Read);

                    byte[] bytes = Encoding.ASCII.GetBytes(log.ToString());

                    bytesWritten = bytes.Length;
                    isWritingLog = true;

                    LogFileStream.BeginWrite(bytes, 0, bytesWritten, new AsyncCallback(LogWriteCompleted), null);
                }
                catch (IOException ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        void LogWriteCompleted(object result)
        {
            isWritingLog = false;
            log.Remove(0, bytesWritten);
            LogFileStream.Flush();
            LogFileStream.Close();
        }

        void createForm()
        {
            //prevent user from creating more then one form
            if (!formPresent)
            {
                formPresent = true;
                form = new FormMain();

                //use close event to reset formpresent boolean
                form.FormClosed += new FormClosedEventHandler(form_FormClosed);
            }
        }

        void form_FormClosed(object sender, FormClosedEventArgs e)
        {
            formPresent = false;
            form = null;

            Log("Closing Clipboard Manager " + DateTime.Now.ToString("H:m:s MM/dd"));
        }

        void IDisposable.Dispose()
        {

        }

    }

}
